package com.example.my.shop.mybatis.controller;

import com.example.my.shop.mybatis.entity.TbUser;
import com.example.my.shop.mybatis.service.TbUserService;
import com.example.my.shop.mybatis.service.impl.TbUserServiceImpl;
import com.example.my.shop.mybatis.utils.CookieUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

 /**
  * @ProjectName:
  * @Package:        com.example.my.shop.mybatis.controller
  * @ClassName:      LoginController
  * @Description:    登录
  * @Author:         Mr.Vincent
  * @CreateDate:     2019/5/29 14:17
  * @Version:        1.0.0
  */
@WebServlet(value = "/login")
public class LoginController extends HttpServlet {

    String cookieName = "UserInfo";
    String userSession = "userSession";

    /**
     * @Method:        doGet
     * @Description:    检查游览器是否有Cookie,有则回显到login.jsp页面
     * @Param:         [req, resp]
     * @return:        void
     * @Author:        Mr.Vincent
     * @Date:          2019/5/29
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cookieValue = CookieUtils.getCookieValue(req, cookieName);

        //判断游览器是否有Cookie,有则回显
        if (StringUtils.isNotBlank(cookieValue)) {
            String[] split = StringUtils.split(cookieValue,":");
            String email = split[0];
            String password = split[1];
            String rememberMe = "checked";
            req.setAttribute("email",email);
            req.setAttribute("password",password);
            req.setAttribute("rememberMe",rememberMe);
        }
        req.getRequestDispatcher("/login.jsp").forward(req, resp);
    }

    /**
     * @Method:        doPost
     * @Description:    用户登录
     * @Param:         [req, resp]
     * @return:        void
     * @Author:        Mr.Vincent
     * @Date:          2019/5/29
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String rememberMe = req.getParameter("rememberMe");

        //当不记住密码的时候，删除Cookie
        if(!"on".equals(rememberMe)){
            CookieUtils.deleteCookie(req,resp,cookieName);
        }

        TbUserService tbUserService = new TbUserServiceImpl();
        TbUser tbUser = tbUserService.selectEmailAndPassWord(email);
        //当前用户登录，用户不为空的时候，判断密码是否正确
        if (tbUser != null) {
            //判断密码是否正确,正确则登录
            if (tbUser.getPassWord().equals(DigestUtils.md5Hex(password)) || password.equals(tbUser.getPassWord())) {

                //把Cookie存储到游览器
                if ("on".equals(rememberMe)) {
                    CookieUtils.setCookie(req, resp, cookieName, String.format("%s:%s", email, DigestUtils.md5Hex(password)), 3600 * 24 * 7);
                }

                req.getSession().setAttribute(userSession,tbUser);
                //重定向
                resp.sendRedirect("main.jsp");
            }
            //密码错误则给出信息提示
            else {
                //请求转发
                req.setAttribute("message", "密码错误，请重新输入！！！");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }
        }
        //当前用户不存在给出信息提示
        else {
            //请求转发
            req.setAttribute("message", "账户不存在！！！");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
